#include <fstream>
#include <vector>
#include <iostream>

using namespace std;

int main(int argc, char* argv[]){
  if(argc != 3){
    cerr<<"usage : convert file.obj file.off"<<endl;
    return 1;
  }

  ifstream ifs(argv[1], ios::in);
  char c;
  unsigned int nv;
  unsigned int nt;
  unsigned int ni;

  ifs>>nv;
  ifs>>nt;
  ifs>>ni;

  vector<float>sommets;
  vector<float>textures;
  vector<float>mytex;
  vector<unsigned int>indices;
  
  if(nv > 0) sommets.resize(nv*3);
  if(nt > 0){
    textures.resize(nt*2);
    mytex.resize(nv*2);
  }
  if(ni > 0) indices.resize(ni*3);

  cout<<nv<<" sommets"<<endl;
  cout<<nt<<" tex coords"<<endl;
  cout<<ni<<" faces"<<endl;

  cout<<"READING"<<endl;
  for(unsigned int i(0); i < nv; i++)
    ifs>>c>>sommets[i*3]>>sommets[i*3+1]>>sommets[i*3+2];  
  for(unsigned int i(0); i < nt; i++)
    ifs>>c>>c>>textures[i*2]>>textures[i*2+1];
  for(unsigned int i(0); i < ni; i++){
    ifs>>c;
    int indicetex;

    ifs>>indices[i*3];
    indices[i*3]--;
    if(nt > 0){
      ifs>>c;
      ifs>>indicetex;
      mytex[indices[i*3]*2] = textures[(indicetex-1)*2];
      mytex[indices[i*3]*2+1] = textures[(indicetex-1)*2+1];
    }
 
    ifs>>indices[i*3+1];
    indices[i*3+1]--;
    if(nt > 0){
      ifs>>c;
      ifs>>indicetex;
      mytex[indices[i*3+1]*2] = textures[(indicetex-1)*2];
      mytex[indices[i*3+1]*2+1] = textures[(indicetex-1)*2+1];
    }
    
    ifs>>indices[i*3+2];
    indices[i*3+2]--;
    if(nt > 0){
      ifs>>c;
      ifs>>indicetex;
      mytex[indices[i*3+2]*2] = textures[(indicetex-1)*2];
      mytex[indices[i*3+2]*2+1] = textures[(indicetex-1)*2+1];
    }
  }
    
  cout<<"WRITING"<<endl;
  ofstream ofs(argv[2], ios::out);
  if(nt == 0)
    ofs<<"OFF"<<endl;
  else
    ofs<<"KPO"<<endl;

  ofs<<nv<<" "<<ni<<" 0"<<endl;
  for(unsigned int i = 0; i < nv; i++)
    ofs<<sommets[i*3]<<" "<<sommets[i*3+1]<<" "<<sommets[i*3+2]<<endl;
  if(nt > 0){
    for(unsigned int i = 0; i < nv; i++)
      ofs<<mytex[i*2]<<" "<<mytex[i*2+1]<<endl;
  }
  for(unsigned int i = 0; i < ni; i++)
    ofs<<"3 "<<indices[i*3+2]<<" "<<indices[i*3+1]<<" "<<indices[i*3]<<endl;
  
  cout<<"TERMINATED"<<endl;

  ifs.close();
  ofs.close();

  return 0;
}
